//初始临床信息显示区域
function initClinicInfoTable(clinicInfo,attachmentlist){
	$('#showAttachTable').bootstrapTable();
	
	if(clinicInfo){
		$('#clinicInfo_table').find('span').each(function(index,item){
			var bindname = $(item).attr('bindname');
			if(bindname){
				$(item).html(transItemCode(bindname,clinicInfo[bindname]));
			}
		})
	}
	
	if(attachmentlist && attachmentlist.length>0){
		loadShowAttachTable(null,attachmentlist);
	}
}

//初始化表单项
function initClinicInfoForm(clinicInfo,attachmentlist){
	$('#clinicInfoForm').validation();
	$('#editAttachTable').bootstrapTable();
	if(clinicInfo){
		$('#clinicInfoForm').bindData(clinicInfo);
	}
	
	if(attachmentlist && attachmentlist.length>0){
		loadEditAttachTable(null,attachmentlist);
	}
	
	$('#file').fileinput({//附件上传
		language : 'zh',
		uploadAsync: false, //默认异步上传
		showPreview :true, //是否显示预览
		showUpload:false, //是否显示上传按钮
		dropZoneEnabled: false,//是否显示拖拽区域
		maxFileCount: 10, //表示允许同时上传的最大文件个数
		enctype: 'multipart/form-data',
	    validateInitialCount:true
	})
	
	//患者知情同意书模板下载
	$('#agreeBookDownload').click(function(){
		$.download('/attachment/agreeBook/download/');
	})
}

//加载可编辑的附件列表
function loadEditAttachTable(conId,attachList){
	if(conId){
		$.getApiClient('/attachment/{business_id}').setQuery({
			business_id:conId
		}).get().done(function(data){
			initTable(data);
		});
	}else{
		initTable(attachList);
	}
	
	function initTable(tableData){
		$('#editAttachTable').bootstrapTable('load',tableData);
	}
}

//加载可编辑的附件列表
function loadShowAttachTable(conId,attachList){
	if(conId){
		$.getApiClient('/attachment/{business_id}').setQuery({
			business_id:conId
		}).get().done(function(data){
			initTable(data);
		});
	}else{
		initTable(attachList);
	}
	
	function initTable(tableData){
		$('#showAttachTable').bootstrapTable('load',tableData);
	}
}

function editAttachTableFormatter(value, row, index){
	var operates=[];
	operates.push('<a class="delete" href="javascript:void(0)" title="删除"><i class="fa fa-close"></i>删除</a>');
	operates.push('&nbsp;&nbsp;&nbsp;');
	operates.push('<a class="download" href="javascript:void(0)" title="下载"><i class="fa fa-download"></i>下载</a>');
	return operates.join('');
}

function showAttachTableFormatter(value, row, index){
	var operates=[];
	operates.push('<a class="download" href="javascript:void(0)" title="下载"><i class="fa fa-download"></i>下载</a>');
	return operates.join('');
}

window.editAttachTableEvents = {
		'click .delete': function (e, value, row, index) {
			doDeleteAttach(row.id,function(data){
				if(data.code == "0"){
					customTimeOutDialog('信息提示','删除附件成功',null,2000);
					$('#editAttachTable').bootstrapTable('removeByUniqueId',row.id);
				}else{
					customAlertDialog(data.message, '错误提示', null, null);
				}
			});
	    },
	    'click .download': function (e, value, row, index) {
			doDownloadAttach(row.id);
	    }
};

window.showAttachTableEvents = {
	    'click .download': function (e, value, row, index) {
			doDownloadAttach(row.id);
	    }
};

//删除附件
function doDeleteAttach(attachId,callback){
	customConfirmDialog('是否确认删除附件？','删除附件',null,function(dialog){
		//执行删除
		dialog.close();
		$.getApiClient("/attachment/delete/{id}").setQuery({
			id : attachId
		}).del().done(function(data){
			callback(data);
		});
	});
}

//下载附件
function doDownloadAttach(attachId){
	$.download('/attachment/download/'+attachId);
}